package com.soar.recruitment.module.me.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.soar.recruitment.R;
import com.soar.recruitment.common.ui.fragment.SOARBaseFragment;
import com.soar.recruitment.common.ui.view.ptr.PtrFrameInit;
import com.soar.recruitment.common.ui.view.ptr.RecyclerViewInit;
import com.soar.recruitment.module.me.dal.bean.MeModel;
import com.soar.recruitment.module.me.ui.adapter.MeAdapter;
import com.soar.recruitment.module.user.ui.activity.LoginActivity;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * 我的
 * Created by chriswen on 2017/6/1.
 */
public class MeFragment extends SOARBaseFragment {

    @BindView(R.id.toolbar_title)
    TextView toolbar_title;
    @BindView(R.id.container_ptr_frame)
    PtrFrameLayout containerPtrFrame;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private View mHeaderView;
    private View mFooterView;

    private HeaderAndFooterWrapper headerAndFooterWrapper;

    private View userInfoContainer;

    private MeAdapter mMeAdapter;
    private List<MeModel> mMeModleList = new ArrayList<>();

    public static MeFragment newInstance() {
        Bundle args = new Bundle();
        MeFragment fragment = new MeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_me;
    }

    @Override
    public void initVariable() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        toolbar_title.setText("我");

        initRecycleView();

        LayoutInflater layoutInflater = LayoutInflater.from(mActivity);
        mHeaderView = layoutInflater.inflate(R.layout.header_me, recyclerView, false);
        mFooterView = layoutInflater.inflate(R.layout.footer_me, recyclerView, false);
        mMeAdapter = new MeAdapter(_mActivity, R.layout.item_me, mMeModleList);
        headerAndFooterWrapper = new HeaderAndFooterWrapper(mMeAdapter);
        headerAndFooterWrapper.addHeaderView(mHeaderView);
        recyclerView.setAdapter(headerAndFooterWrapper);

        userInfoContainer = mHeaderView.findViewById(R.id.userInfoContainer);
        userInfoContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity, LoginActivity.class));
            }
        });
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecycleView() {
        PtrFrameInit.init(containerPtrFrame, mActivity, false);
        containerPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

            }
        });
        containerPtrFrame.disableWhenHorizontalMove(true);
        containerPtrFrame.autoRefresh(true);

        RecyclerViewInit.init(recyclerView, mActivity);
    }

    @Override
    public void initData() {
        MeModel meModel = null;
        for(int i = 0; i < 5; i++) {
            meModel = new MeModel();
            mMeModleList.add(meModel);
        }
        mMeAdapter.setData(mMeModleList);

        if (headerAndFooterWrapper.getFootersCount() > 0) {
            headerAndFooterWrapper.removeFootView(mFooterView);
        }
        headerAndFooterWrapper.addFootView(mFooterView);
    }
}
